{% extends 'web/layout/basic.html' %}
{% load static %}
{% block title %} 用户注册 {% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户注册</div>
        <form id="regForm" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="注  册" />
                </div>
            </div>
        </form>
    </div>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickBtnSumbit();
        });

        //注册
        function bindClickBtnSumbit() {

            $("#btnSubmit").click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url:'{%url "web:register" %}',
                    type:'post',
                    dataType:'json',
                    data:$("#regForm").serialize(),
                    success:function (res) {
                        if(res.status){
                            console.log(res.data);
                            location.href=res.data;
                        }else{
                            $.each(res.error,function (key,value) {
                                $("#id_"+key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        };

        //校验手机号码
        function bindClickBtnSms() {
            $(".error-msg").empty();
            $("#btnSms").click(function () {
                let mobilePhone = $('#id_mobile_phone').val();
                $.ajax({
                    url:'{% url "web:sms_send" %}',
                    type:'get',
                    dataType:'json',
                    data:{mobile_phone:mobilePhone,tpl: "register"},
                    success:function (res) {
                        if(res.status){
                            console.log(res);
                            sendSmsRemind();
                        }else{
                            {#console.log(res['error']);#}
                            $.each(res.error,function (key,value) {
                                $("#id_"+key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }
        // 验证码倒计时
        function sendSmsRemind() {
            let $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true); // 禁用
            let time=60;
            let remind=setInterval(function () {
                $smsBtn.val(time+'秒后重新发送');
                time-=1;
                if(time<1){
                    clearInterval(remind);
                    $smsBtn.val('点击发送验证码').prop('disabled',false);
                }
            },1000)
        }
    </script>
{% endblock %}